//
//  HomeTestViewController.m
//  Koizora
//
//  Created by dlios on 15-6-8.
//  Copyright (c) 2015年 CupCat. All rights reserved.
//

#import "HomeTestViewController.h"
#import "TestHomeTableViewCell.h"
#import "TestDetailViewController.h"
#import "NetworkHandle.h"
#import "HomeTestModel.h"
#import <UIImageView+WebCache.h>
#import "TestzhuanjiaCollectionViewCell.h"

@interface HomeTestViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *tableVView;
@property (nonatomic, strong) HomeTestModel *testModel;
@property (nonatomic, strong) NSMutableArray *arr;
//@property (nonatomic, strong) TestFunView *funView;

@end

@implementation HomeTestViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createTableView];
    [self getData];
    UISegmentedControl *exchange = [[UISegmentedControl alloc] initWithItems:@[@"爱情",@"趣味"]];
    exchange.frame = CGRectMake(self.view.frame.size.width / 3, 64, self.view.frame.size.width / 3, 30);
    exchange.selectedSegmentIndex = 0;
    [exchange addTarget:self action:@selector(jump:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:exchange];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}
- (void)createTableView
{

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 90;
    [self.view addSubview:_tableView];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
        TestHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[TestHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        [cell.coverImg1 sd_setImageWithURL:[NSURL URLWithString:_testModel.coverImg]];
        cell.testTitle1.text = [_arr[indexPath.row] testTitle];
        [cell.coverImg1 sd_setImageWithURL:[NSURL URLWithString:[_arr[indexPath.row] coverImg]]];
        return cell;
        
    } else {
        
        TestHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user"];
        if (cell == nil) {
            cell = [[TestHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"user"];
        }
        [cell.coverImg1 sd_setImageWithURL:[NSURL URLWithString:_testModel.coverImg]];
        cell.testTitle1.text = [_arr[indexPath.row] testTitle];
        [cell.coverImg1 sd_setImageWithURL:[NSURL URLWithString:[_arr[indexPath.row] coverImg]]];
        return cell;
    }
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestDetailViewController *testDetail = [[TestDetailViewController alloc] init];
    testDetail.index = indexPath;
    testDetail.testID = [_arr[indexPath.row] testId];
    [self.navigationController pushViewController:testDetail animated:YES];
    
}

- (void)getData
{
    [NetworkHandle getDataWithURL:@"http://star.zhuoyouapp.com/v1/questionnaireServer/getQuestionnaireList?page=1&rows=10&categoryId=93" completion:^(id result) {
        self.arr = [NSMutableArray array];
        NSMutableArray *ar = [NSMutableArray array];
        ar = [result objectForKey:@"rows"];
        for (NSDictionary *dic in ar) {
            HomeTestModel *m = [[HomeTestModel alloc] initWithDic:dic];
            [self.arr addObject:m];
        }
        [_tableView reloadData];
    }];
 
    
}

- (void)jump:(UISegmentedControl *)jump
{
    if (jump.selectedSegmentIndex == 0) {
        [self.tableVView removeFromSuperview];
        [self createTableView];
        [self getData];
        
    } else {
        
        [self.tableView removeFromSuperview];
        [self createTableVView];
        [self getDData];
    }
   
}

- (void)createTableVView
{
    
    self.tableVView = [[UITableView alloc]initWithFrame:CGRectMake(0,  94, self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain];
    _tableVView.delegate = self;
    _tableVView.dataSource = self;
    _tableVView.rowHeight = 90;
    [self.view addSubview:_tableVView];
}

- (void)getDData
{
    [NetworkHandle getDataWithURL:@"http://star.zhuoyouapp.com/v1/questionnaireServer/getQuestionnaireList?page=1&rows=10&categoryId=106" completion:^(id result) {
        self.arr = [NSMutableArray array];
        NSMutableArray *ar = [NSMutableArray array];
        ar = [result objectForKey:@"rows"];
        for (NSDictionary *dic in ar) {
            HomeTestModel *m = [[HomeTestModel alloc] initWithDic:dic];
            [self.arr addObject:m];
        }
        [_tableVView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
